﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
using MLY.Bll;
using MLY.Dal;

namespace MLY
{
    internal static class DevExpress_ExtensionMethods
    {
        //ASPxGridViewColumnDataEventArgs
        //int workOrderId = (int)e.GetListSourceFieldValue(TableWorkOrder.cnWorkOrderId);

        internal static int GetWorkOrderID(this ASPxGridViewColumnDataEventArgs e)
        {
            return (int)e.GetListSourceFieldValue(TableWorkOrder.cnWorkOrderId);
        }

        internal static void SetNewValueStr(this ASPxDataUpdatingEventArgs e, string fieldName, string value)
        {
            e.NewValues[fieldName] = value;
        }

        internal static void SetNewValueStr(this ASPxDataInsertingEventArgs e, string fieldName, string value)
        {
            e.NewValues[fieldName] = value;
        }

        internal static void SetNewValueInt(this ASPxDataUpdatingEventArgs e, string fieldName, int value)
        {
            e.NewValues[fieldName] = value;
        }

        internal static void SetNewValueIntNull(this ASPxDataUpdatingEventArgs e, string fieldName)
        {
            e.NewValues[fieldName] = null;
        }

        internal static void SetNewValueInt(this ASPxDataInsertingEventArgs e, string fieldName, int value)
        {
            e.NewValues[fieldName] = value;
        }

        internal static void SetNewValueIntNull(this ASPxDataInsertingEventArgs e, string fieldName)
        {
            e.NewValues[fieldName] = null;
        }

        internal static void SetNewValueDecimal(this ASPxDataUpdatingEventArgs e, string fieldName, decimal value)
        {
            e.NewValues[fieldName] = value;
        }

        internal static void SetDateTime(this OrderedDictionary orderedDictionary,
                                         string fieldName, DateTime? value)
        {
            // ORIGINAL CODE:
            //if (value <= Utils.MinSqlDateTime || value >= Utils.MaxSqlDateTime)
            //  return;
            //orderedDictionary[fieldName] = value;
            // END OF ORIGINAL CODE

            if (value <= Utils.MinSqlDateTime || value >= Utils.MaxSqlDateTime)
                orderedDictionary[fieldName] = null;
            else
                orderedDictionary[fieldName] = value;
        }

        internal static void SetDateTime(this OrderedDictionary orderedDictionary, string fieldName,
                                         DateTime value, DateTime? defaultValue)
        {
            if (value <= Utils.MinSqlDateTime || value >= Utils.MaxSqlDateTime)
                orderedDictionary[fieldName] = defaultValue;
            else
                orderedDictionary[fieldName] = value;
        }

        internal static void SetNewValueDateTime(this ASPxDataUpdatingEventArgs e, string fieldName,
                                                 DateTime value, DateTime? defaultValue)
        {
            e.NewValues.SetDateTime(fieldName, value, defaultValue);
        }

        internal static void SetNewValueDateTime(this ASPxDataUpdatingEventArgs e, string fieldName,
                                                 DateTime? value)
        {
            e.NewValues.SetDateTime(fieldName, value);
        }

        internal static void SetNewValueDateTime(this ASPxDataInsertingEventArgs e, string fieldName, DateTime value)
        {
            e.NewValues.SetDateTime(fieldName, value);
        }

        internal static void ASPxDataInsertingEventArgs(this ASPxDataUpdatingEventArgs e, string fieldName, DateTime value)
        {
            e.NewValues.SetDateTime(fieldName, value);
        }

        //internal static void AutoFilterByColumnName(this ASPxGridView grid, string columnName, string value)
        //{
        //  if (grid == null || string.IsNullOrEmpty(columnName) || string.IsNullOrEmpty(value))
        //    return;
        //  object obj = grid.Columns["columnName"];
        //  if (obj is GridViewColumn)
        //  {
        //    GridViewColumn column = (GridViewColumn)obj;
        //    grid.AutoFilterByColumn(column, value);
        //  }
        //}

    }
}
